package com.yupi.springbootinit.mq.TopicExchange;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;

public class TopicProducer {
    //1:定义交换机的名称
    public static final String EXCHANGE_NAME = "topic_exchange";

    public static void main(String[] args) {
        //2：创建连接工厂，指定信息
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.1.183");
        factory.setUsername("lfc");
        factory.setPassword("123456");
        //3：创建连接
        try {
            Connection connection = factory.newConnection();
            //4：创建频道
            Channel channel = connection.createChannel();
            //5：声明交换机，名称以及类型
            channel.exchangeDeclare(EXCHANGE_NAME, "topic");
            Scanner scanner = new Scanner(System.in);
            while (scanner.hasNext()) {
                String message = scanner.nextLine();
                //6：绑定交换机，向哪个交换机发送
                channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("UTF-8"));
                System.out.println(" [x] Sent '" + message + "'");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
